package com.gxa.xiaoucomeshome.service.impl;

import com.gxa.xiaoucomeshome.exception.BusinessException;
import com.gxa.xiaoucomeshome.exception.ExceptionCode;
import com.gxa.xiaoucomeshome.mapper.AdminMapper;
import com.gxa.xiaoucomeshome.pojo.*;
import com.gxa.xiaoucomeshome.service.AdminService;
import com.gxa.xiaoucomeshome.vo.*;
import io.swagger.models.auth.In;
import org.apache.ibatis.annotations.Param;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.ControllerAdvice;

import java.util.List;

@ControllerAdvice
public class AdminServiceImpl implements AdminService {
    @Autowired
    AdminMapper adminMapper;

    /**
     * 超级管理员登录
     *
     * @param admin_phone
     * @param admin_pwd
     * @return
     * @throws BusinessException
     */
    @Override
    public Admin login(String admin_phone, String admin_pwd) throws BusinessException {
        //TODO pwd进行MD5加密加盐处理
        Admin admin = adminMapper.adminLogin(admin_phone, admin_pwd);
        if (admin == null) {
            throw new BusinessException(ExceptionCode.USER_FAILED);
        }
        System.out.println("admin:" + admin);
        return admin;
    }

    /**
     * 展示首页数据
     *
     * @return
     * @throws BusinessException
     */
    @Override
    public InfoVo showInfo() throws BusinessException {
        InfoVo infoVo = adminMapper.showInfo();
        System.out.println("infoVo:" + infoVo);
        return infoVo;
    }

    /**
     * 添加用户协议
     *
     * @param agree_desc
     * @return
     * @throws BusinessException
     */
    @Override
    public void userAgree(String agree_desc) throws BusinessException {
        adminMapper.userAgree(agree_desc);
    }

    /**
     * 名字模糊分页查询门店
     *
     * @param page
     * @param size
     * @param site_name
     * @return
     */
    @Override
    public List<Site> selectSites(Integer page, Integer size, String site_name) {
        List<Site> sites = adminMapper.selectSites(page, size, site_name);
        return sites;
    }

    /**
     * 查询站点数量
     *
     * @param site_name
     * @return
     */
    @Override
    public int selectSitesCount(String site_name) {
        int total = adminMapper.selectSitesCount(site_name);
        return total;
    }

    /**
     * 站点批量删除
     *
     * @param site_id
     * @return
     */
    @Override
    public int deleteSite(String[] site_id) {
        int i = adminMapper.deleteSite(site_id);
        return i;
    }

    /**
     * 站点单个删除
     *
     * @param site_id
     * @return
     */
    @Override
    public int deleteSiteById(Integer site_id) {
        int i = adminMapper.deleteSiteById(site_id);
        return i;
    }

    /**
     * 根据编号查询站点
     *
     * @param site_id
     * @return
     */
    @Override
    public Site selectSiteById(Integer site_id) {
        Site site = adminMapper.selectSiteById(site_id);
        return site;
    }

    /**
     * 添加站点
     *
     * @param site
     */
    @Override
    public void addSite(Site site) {
        adminMapper.addSite(site.getSite_name(), site.getSite_phone(),
                site.getSite_spare_phone(), site.getSite_desc(),
                site.getSite_start_time(), site.getSite_end_time(),
                site.getSite_address(), site.getSite_ambient_desc(),
                site.getSite_ambient_range(), site.getSite_ambient_state(),
                site.getSite_fresh_desc(), site.getSite_fresh_range(), site.getSite_fresh_state());
    }

    /**
     * 添加站员
     *
     * @param manager
     */
    @Override
    public void addManager(Manager manager) {
        adminMapper.addManager(manager);
    }

    /**
     * 查询所有的地址
     *
     * @return
     */
    @Override
    public List<SiteAddress> selectAddress() {
        List<SiteAddress> siteAddresses = adminMapper.selectAddress();
        return siteAddresses;
    }

    /**
     * 配送员模糊查询分页
     *
     * @param page
     * @param size
     * @param phone
     * @param state
     * @return
     */
    @Override
    public List<Courier> selectCourier(Integer page, Integer size, String phone, Integer state) {
        List<Courier> couriers = adminMapper.selectCourier(page, size, phone, state);
        return couriers;
    }

    /**
     * 配送员数量
     *
     * @param phone
     * @param state
     * @return
     */
    @Override
    public Integer selectCourierCount(String phone, Integer state) {
        int total = adminMapper.selectCourierCount(phone, state);
        return total;
    }

    /**
     * 修改站点信息
     *
     * @param site
     */
    @Override
    public void editSite(Site site) {
        adminMapper.editSite(site.getSite_id(), site.getSite_name(), site.getSite_phone(),
                site.getSite_spare_phone(), site.getSite_desc(),
                site.getSite_start_time(), site.getSite_end_time(),
                site.getSite_address(), site.getSite_ambient_desc(),
                site.getSite_ambient_range(), site.getSite_ambient_state(),
                site.getSite_fresh_desc(), site.getSite_fresh_range(), site.getSite_fresh_state());
    }

    /**
     * 查询站员分页
     *
     * @param page
     * @param size
     * @param phone
     * @return
     */
    @Override
    public List<Manager> selectManager(Integer page, Integer size, String phone) {
        List<Manager> managers = adminMapper.selectManager(page, size, phone);
        return managers;
    }

    /**
     * 站员数量
     *
     * @param phone
     * @return
     */
    @Override
    public Integer selectManagerCount(String phone) {
        int total = adminMapper.selectManagerCount(phone);
        return total;
    }

    /**
     * 编辑站员信息
     *
     * @param manager
     */
    @Override
    public void editManager(Manager manager) {
        adminMapper.editManager(manager.getName(), manager.getPhone(), manager.getSite(),manager.getId());
    }

    /**
     * 站员批量删除
     * @param mgr_id
     * @return
     */
    @Override
    public int deleteManager(String[] mgr_id) {
        int i = adminMapper.deleteManager(mgr_id);
        return i;
    }

    /**
     * 根据编号删除站员
     * @param mgr_id
     * @return
     */
    @Override
    public int deleteManagerById(Integer mgr_id) {
        int i = adminMapper.deleteManagerById(mgr_id);
        return i;
    }

    /**
     * 查询所有交通工具
     * @return
     */
    @Override
    public List<Courier> selectVehicle() {
        List<Courier> couriers = adminMapper.selectVehicle();
        return couriers;
    }

    /**
     * 添加配送员
     * @param phone
     * @param password
     * @param coname
     * @param gender
     * @param age
     * @param vehicle
     * @param drivingPermit
     * @param idCard
     * @param drivingLicense
     * @param source
     */
    @Override
    public int addCourice(String phone, String password, String coname, String gender, Integer age, String vehicle, String drivingPermit, String idCard, String drivingLicense, String source) {
        int  i = adminMapper.addCourice(phone,password,coname,gender,
                age,vehicle,drivingPermit,idCard,
                drivingLicense,source);
        return i;
    }

    /**
     * 查询所有门店名
     * @return
     */
    @Override
    public List<Site> selectSiteName() {
        List<Site> sites = adminMapper.selectSiteName();
        return sites;
    }

    /**
     * 配送员批量删除
     * @param phone
     * @return
     */
    @Override
    public int courierDelete(String[] phone) {
        int i = adminMapper.courierDelete(phone);
        return i;
    }

    /**
     * 修改配送员信息
     * @param phone
     * @param password
     * @param coname
     * @param gender
     * @param age
     * @param vehicle
     * @param drivingPermit
     * @param idCard
     * @param drivingLicense
     * @param source
     */
    @Override
    public void editCourier(String newPhone, String password, String coname, String gender, String age, String vehicle, String drivingPermit, String idCard, String drivingLicense, String source,String phone) {
        adminMapper.editCourier(newPhone,password,coname,gender,
                age,vehicle,drivingPermit,idCard,
                drivingLicense,source,phone);
    }

    /**
     * 配送员批量冻结
     * @param phone
     * @return
     */
    @Override
    public int freezeCourier(String[] phone) {
        int i = adminMapper.freezeCourier(phone);
        return i;
    }
    /**
     * 配送员批量解冻
     * @param phone
     * @return
     */
    @Override
    public int thawCourier(String[] phone) {
        int i = adminMapper.thawCourier(phone);
        return i;
    }

    /**
     * 根据编号查询密码
     * @param adminId
     * @return
     */
    @Override
    public String selectPwdById(Integer adminId) {
        String pwd = adminMapper.selectPwdById(adminId);
        return pwd;
    }

    /**
     * 修改密码
     * @param adminId
     * @param newPassword
     * @return
     */
    @Override
    public int updatePwd(Integer adminId, String newPassword) {
        int i = adminMapper.updatePwd(adminId,newPassword);
        return i;
    }

    /**
     * 根据编号查询站员
     * @param mgr_id
     * @return
     */
    @Override
    public ManagerVo selectManagerById(Integer mgr_id) {
        ManagerVo manager = adminMapper.selectManagerById(mgr_id);
        return manager;
    }

    /**
     * 根据电话查询配送员
     * @param phone
     * @return
     */
    @Override
    public Courier selectCourierByPhone(String phone) {
        Courier courier = adminMapper.selectCourierByPhone(phone);
        return courier;
    }

    /**
     * 查询父类商品
     * @return
     */
    @Override
    public List<Commodity> selectCommodity() {
        List<Commodity> commodity = adminMapper.selectCommodity();
        return commodity;
    }

    /**
     * 查询父类商品下面所有的子类
     * @param id
     * @return
     */
    @Override
    public List<GoodsChild> goodsChildList(Integer page,Integer size,Integer id,String goods_child_name) {
        List<GoodsChild> goodsChildList = adminMapper.goodsChildList(page,size,id,goods_child_name);
        return goodsChildList;
    }

    /**
     * 查询商品数量
     * @param id
     * @param goods_child_name
     * @return
     */
    @Override
    public Integer selectGoodsChildCount(int id, String goods_child_name) {
        int total = adminMapper.selectGoodsChildCount(id, goods_child_name);
        return total;
    }

    /**
     * 修改父类信息
     * @param commodity_name
     * @return
     */
    @Override
    public int updateGoods(String commodity_name,Integer id) {
        int i = adminMapper.updateGoods(commodity_name,id);
        return i;
    }

    /**
     * 根据电话删除配送员
     * @param phone
     * @return
     */
    @Override
    public int courierDeleteByPhone(String phone) {
        int i = adminMapper.courierDeleteByPhone(phone);
        return i;
    }

    /**
     * 根据电话冻结配送员
     * @param phone
     * @return
     */
    @Override
    public int freezeCourierByPhone(String phone) {
        int i = adminMapper.freezeCourierByPhone(phone);
        return i;
    }

    /**
     * 根据电话解冻配送员
     * @param phone
     * @return
     */
    @Override
    public int thawCourierByPhone(String phone) {
        int i = adminMapper.nofreezeCourierByPhone(phone);
        return i;
    }
    /**
     * 修改子类信息
     * @param goods_child_name
     * @return
     */
    @Override
    public int updateGoodsChild(String goods_child_name,Integer goods_child_id,Integer goods_id) {
        int i = adminMapper.updateGoodsChild(goods_child_name,goods_child_id,goods_id);
        return i;
    }

    /**
     * 删除子类商品
     * @param goods_child_id
     * @return
     */
    @Override
    public int deleteGoodsChild(Integer goods_child_id) {
        int i = adminMapper.deleteGoodsChild(goods_child_id);
        return i;
    }

    /**
     * 添加父类对象
     * @param commodity_name
     * @return
     */
    @Override
    public int addGoods(String commodity_name) {
        int i = adminMapper.addGoods(commodity_name);
        return i;
    }

    /**
     * 添加子类对象
     * @param goods_child_name
     * @param goods_id
     * @return
     */
    @Override
    public int addGoodsChild(String goods_child_name, Integer goods_id) {
        int i = adminMapper.addGoodsChild(goods_child_name,goods_id);
        return i;
    }

    /**
     * 查询角色
     * @param page
     * @param size
     * @param role_name
     * @return
     */

    @Override
    public List<Role> selectRole(Integer page, Integer size, String role_name) {
        List<Role> roles = adminMapper.selectRole(page, size, role_name);
        return roles;
    }

    @Override
    public Integer selectRoleCount(String role_name) {
        Integer integer = adminMapper.selectRoleCount(role_name);
        return integer;
    }

    /**
     * 删除角色
     * @param role_id
     * @return
     */
    @Override
    public int deleteRole(Integer role_id) {
        int i = adminMapper.deleteRole(role_id);

        return i;
    }

    /**
     * 修改角色
     * @param role_name
     * @param role_desc
     * @param role_id
     * @return
     */
    @Override
    public int updateRole(String role_name, String role_desc, Integer role_id) {
        int i = adminMapper.updateRole(role_name, role_desc, role_id);
        return i;
    }

    @Override
    public List<Menu> selectMenu() {
        List<Menu> menus = adminMapper.selectMenu();
        return menus;
    }
    /**
     * 添加角色
     */
    @Override
    public void addRole(RoleVo roleVo) {
        adminMapper.addRole(roleVo.getRole_name(),roleVo.getRole_desc());
    }
    /**
     * 查询操作员
     * @param page
     * @return
     */
    @Override
    public List<AdminRoleVo> selectAdminRole(Integer page, Integer size, String manage, Integer role_id) {
        List<AdminRoleVo> adminRoleVos = adminMapper.selectAdminRole(page, size, manage, role_id);
        return adminRoleVos;
    }

    @Override
    public Integer selectAdminRoleCount(String manage, Integer role_id) {
        Integer integer = adminMapper.selectAdminRoleCount(manage, role_id);
        return integer;
    }

    /**
     * 新增操作员
     */
    @Override
    public void addAdminRole(AdminRoleVo roleVo) {
        adminMapper.addAdminRole(roleVo.getAdmin_phone(),roleVo.getAdmin_name(),roleVo.getRole_id());
    }
    /**
     * 删除操作员
     * @return
     */
    @Override
    public int deleteAdminRole(Integer admin_id) {
        int i = adminMapper.deleteAdminRole(admin_id);
        return i;
    }
    /**
     * 修改操作员
     * @return
     */
    @Override
    public int updateAdminRole(String admin_phone, String admin_name, Integer role_id, Integer admin_id) {
        int i = adminMapper.updateAdminRole(admin_phone, admin_name, role_id, admin_id);
        return i;
    }
    /**
     * 重置密码
     * @param admin_id
     * @return
     */
    @Override
    public int updatePasswordAdmin(Integer admin_id) {
        int i = adminMapper.updatePasswordAdmin(admin_id);
        return i;
    }


}